package gjw.recycleviewdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import gjw.recycleviewdemo.R;

/**
 * @创建者 Jimven
 * @创建时间 2017/12/27/0027  10:13
 */
public class RecycleviewAdapter extends RecyclerView.Adapter<RecycleviewAdapter.ViewHolder> {
    private List    mTitle;
    private Context mContext;

    public RecycleviewAdapter(List mTitle, Context mContext) {
        this.mTitle = mTitle;
        this.mContext = mContext;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_gridview_discover, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.allView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,mTitle.get(viewHolder.getAdapterPosition()).toString() , Toast.LENGTH_SHORT).show();

            }
        });

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTv_name.setText(mTitle.get(position).toString());

    }

    @Override
    public int getItemCount() {
        return mTitle == null ? 0 : mTitle.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private       View      allView;
        private final ImageView mIv_icon;
        private final TextView  mTv_name;


        public ViewHolder(View itemView) {
            super(itemView);
            allView = itemView;
            mIv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
            mTv_name = (TextView) itemView.findViewById(R.id.tv_name);

        }
    }
}
